/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.polytec.controller;

import com.polytec.beans.CompteFacade;
import com.polytec.controller.util.JsfUtil;
import com.polytec.entities.Compte;
import com.polytec.entities.Panier;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Felix
 */
@ManagedBean
@SessionScoped
public class LoginController implements Serializable {

    /**
     * Creates a new instance of LoginController
     */
    @ManagedProperty(value = "#{panierController}")
    private PanierController panierController;

    public LoginController() {
    }
    @EJB
    private CompteFacade compteFacade;
    private String userName;
    private String password;
    private String repassword;
    private Panier current;

    public String getRepassword() {
        return repassword;
    }

    public void setRepassword(String password) {
        this.repassword = repassword;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String inscrip() {
        Compte compte = compteFacade.findByUserName(userName);
        if (compte == null) {
            JsfUtil.addSuccessMessage("User name is available");
            return null;
        } else {
            JsfUtil.addErrorMessage("username is already used");
            return null;
        }

//        if (!this.getPassword() equals.(this.getRepassword())) {
//            JsfUtil.addErrorMessage("passowrd doesn't match");
//            return null;
//        }
//        
//        if (this.getPassword().length() < 5) {
//            JsfUtil.addErrorMessage("passowrd too short");
//            return null;
//        }
    }

    public String signIn() {
        Compte compte = compteFacade.findByUserName(userName);
        if (compte == null) {
            //System.err.println("username not found");
            JsfUtil.addErrorMessage("username not found");

            return null;
        } else {


            if (!compte.getMotDePasse().equals(password)) {
                //System.err.println("password incorrect");
                JsfUtil.addErrorMessage("password incorrect");
                return null;
            } else {
                //System.out.println("Sign in succeded");
                JsfUtil.addSuccessMessage("Sign in succeded");
                createPanier(compte);
                return "Accueil.xhtml?faces-redirect=true";

            }
        }
    }

    public void createPanier(Compte compte) {
        current = new Panier();
        current.setLogin(compte);
        current.setSommePanier(0);
        panierController.create(current);
    }

    public PanierController getPanierController() {
        return panierController;
    }

    public void setPanierController(PanierController panierController) {
        this.panierController = panierController;
    }

    public Panier getCurrent() {
        return current;
    }

    public void setCurrent(Panier current) {
        this.current = current;
    }

    public String findName() {
        String compte;
        compte = compteFacade.findByName(userName);
        return (compte);
    }
}
